package jinyilw.swing.hint;

import jinyilw.common.TextTools;
import jinyilw.swing.WindowTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.i18n.IString;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.visuals.window.TransWindow;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.border.Border;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.concurrent.ConcurrentHashMap;

public class HintWindow extends TransWindow implements Runnable
{
	private static final long serialVersionUID = -3380500239623080257L;

	private static final ConcurrentHashMap<String, HintWindow> windows = new ConcurrentHashMap<>();
	private static final Border border = BorderFactory.createEmptyBorder(6, 10, 6,
			10);
	private static final int delay = 1300;
	private static final int arc = 8;
	private Component parent = null;
	private Point position = null;
	private final JLabel textLabel = new JLabel();
	private boolean isShowing;

	private final RenderingConfig renderingConfig = new RenderingConfig();

	public HintWindow(String message)
	{
		super(null);
		setAlwaysOnTop(true);
		setLayout(new BorderLayout());
		add(textLabel, BorderLayout.CENTER);
		textLabel.setText(message);
		// textLabel.setForeground(Color.black);
		textLabel.setBorder(border);
		setSize(textLabel.getPreferredSize());
	}
	public HintWindow(String message, Component com)
	{
		this(message);
		parent = com;
	}

	public void setParent(Component parent)
	{
		this.parent = parent;
	}

	public void setPosition(Point p)
	{
		position = p;
	}

	@Override
	public void run()
	{
		if (isShowing)
			return;
		isShowing = true;
		try
		{
			Thread.sleep(delay);
			for (float i = 0.95f; i >= 0; i -= 0.05f)
			{
				WindowTools.setWindowOpacity(this, i);
				Thread.sleep(70);
			}
		} catch (Exception ignored)
		{
		}
		dispose();
		setOpacity(1f);
		isShowing = false;
	}

	@Override
	protected void paintBackground(Graphics g, int width, int height)
	{
		Graphics2D g2d = (Graphics2D) g;
		renderingConfig.setAAPainting(g2d);
		g2d.setColor(RThemeColor.colorSet.transBGColor);
		g2d.fillRoundRect(1, 1, width - 2, height - 2, arc, arc);
		if (!UITools.isLight())
		{
			g2d.setColor(RThemeColor.colorSet.borderColor);
			g2d.drawRoundRect(1, 1, width - 2, height - 2, arc, arc);
		}
		renderingConfig.resetAAPainting(g2d);
	}

	public HintWindow showItNow()
	{
		// setSize(textLabel.getPreferredSize());
		// pack();
		if (position == null || (position.x < 0 && position.y < 0))
			setLocationRelativeTo(parent);
		else
			setLocation(position);
		setVisible(true);
		new Thread(this).start();
		return this;
	}

	private static HintWindow getHintWindow(String message)
	{
		if (windows.containsKey(message))
			return windows.get(message);
		HintWindow hint = new HintWindow(message);
		windows.put(message, hint);
		return hint;
	}

	public static void showHint(String message)
	{
		showHint(message, null);
	}

	public static void showHint(String message, Component com)
	{
		if (TextTools.isBlank(message))
			return;
		HintWindow hint = getHintWindow(message);
		hint.setParent(com);
		hint.showItNow();
	}

	public static void showHint(IString iMessage, Component com)
	{
		if (iMessage != null)
			showHint(iMessage.getI18nString(), com);
	}
}
